package com.itxckf.controller;


import com.itxckf.pojo.Products;
import com.itxckf.pojo.Result;
import com.itxckf.service.CartService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@Slf4j
@RestController
public class CartController {
    @Autowired
    private CartService cartService;

    /**
     * 插入到购物车
     * @param addCartFrom
     * @return
     */
    @PostMapping("/addCart")
    public Result addCart(@RequestBody AddCartFrom addCartFrom){
        log.info("添加购物车，{}",addCartFrom);
        cartService.addCart(addCartFrom.getUserID(),addCartFrom.getProductID());
        return Result.success("添加成功！");
    };


    @PostMapping("/getCart")
    public Result getCart(@RequestBody AddCartFrom addCartFrom){
        log.info("获取购物车，{}",addCartFrom);
        List<Products> productsList =  cartService.getCart(addCartFrom.getUserID());
        return Result.success(productsList);
    };
}
@Data
@NoArgsConstructor
@AllArgsConstructor
class AddCartFrom {
    private Short userID;
    private Short productID;

}
